package controllers

import (
	"Pj/models"
	"Pj/utils"
	"github.com/astaxie/beego"
	"strconv"
)

type PjArticleController struct {
	beego.Controller
}

func (c *PjArticleController) Get() {
	// Data
	var tpl utils.Tpl
	tpl.Title = "PjArticle"
	tpl.Attributes = models.PjArticleAttributes()
	tpl.Model, _ = models.GetAllPjArticle()
	tpl.Success = c.GetSession("success")
	tpl.Danger = c.GetSession("danger")
	c.DelSession("success")
	c.DelSession("danger")
	c.Data["Tpl"] = tpl
	// Page
	c.Layout = "public/tpl.html"
	c.TplName = "article/index.html"
}

// Create ...
func (c *PjArticleController) Create() {
	var m models.PjArticleModel

	// Post 表单提交
	if c.Ctx.Input.IsPost() {
		m.ArticleTitle = c.GetString("article_title")
		m.TypeId, _ = c.GetInt("type_id")

		num, err := models.CreatePjArticle(&m)
		if err != nil {
			utils.ToLog("Error", err.Error(), 3)
			c.SetSession("danger", err.Error())
		} else {
			utils.ToLog("Success", "Create ID:"+strconv.Itoa(num), 5)
			c.SetSession("success", "Create ID:"+strconv.Itoa(num))
			c.Redirect("/article/view?article_id="+strconv.Itoa(m.ID), 302)
		}
	}

	// Data
	var tpl utils.Tpl
	tpl.Title = "PjArticle Create"
	tpl.Attributes = models.PjArticleAttributes()
	tpl.Action = c.URLFor("PjArticleController.Create")
	tpl.Model = m
	c.Data["Tpl"] = tpl
	// Page
	c.Layout = "public/tpl.html"
	c.TplName = "article/create.html"
}

// Update ...
func (c *PjArticleController) Update() {

	id, _ := c.GetInt("article_id")

	// Post 表单提交
	if c.Ctx.Input.IsPost() {
		var m models.PjArticleModel
		m.ID, _ = c.GetInt("article_id")
		m.ArticleTitle = c.GetString("article_title")
		m.TypeId, _ = c.GetInt("type_id")

		err := models.UpdatePjArticle(&m)
		if err != nil {
			utils.ToLog("Error", err.Error(), 3)
			c.SetSession("danger", err.Error())
			c.Redirect("/article/view?article_id="+strconv.Itoa(m.ID), 302)
		} else {
			utils.ToLog("Success", "Update ID:"+strconv.Itoa(m.ID), 5)
			c.SetSession("success", "Update ID:"+strconv.Itoa(m.ID))
			c.Redirect("/article/view?article_id="+strconv.Itoa(m.ID), 302)
		}
	}

	// Data
	var tpl utils.Tpl
	tpl.Title = "PjArticle Update"
	tpl.Attributes = models.PjArticleAttributes()
	tpl.Model, _ = models.GetPjArticleById(id)
	tpl.Action = c.URLFor("PjArticleController.Update")
	c.Data["Tpl"] = tpl
	// Page
	c.Layout = "public/tpl.html"
	c.TplName = "article/update.html"
}

// View ...
func (c *PjArticleController) View() {
	id, _ := c.GetInt("article_id")
	// Data
	var tpl utils.Tpl
	tpl.Title = "PjArticle View"
	tpl.Attributes = models.PjArticleAttributes()
	tpl.Model, _ = models.GetPjArticleById(id)
	tpl.Action = c.URLFor("PjArticleController.View")
	tpl.Success = c.GetSession("success")
	tpl.Danger = c.GetSession("danger")
	c.DelSession("success")
	c.DelSession("danger")
	c.Data["Tpl"] = tpl
	// Page
	c.Layout = "public/tpl.html"
	c.TplName = "article/view.html"
}

// Del ...
func (c *PjArticleController) Del() {
	id, _ := c.GetInt("article_id")
	_, err := models.DeletePjArticleById(id)
	if err != nil {
		utils.ToLog("Error", err.Error(), 3)
		c.SetSession("danger", err.Error())
	} else {
		utils.ToLog("Success", "Delete ID:"+strconv.Itoa(id), 5)
		c.SetSession("success", "Delete ID:"+strconv.Itoa(id))
	}
	c.Redirect("/article", 302)
}
